//
//  UserViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/13.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "UserViewController.h"

#import "NewTableViewCell.h"
#import "NewViewController.h"

@interface UserViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tabelView;

//轮播图
@property (nonatomic, strong) UIScrollView * lunbo_scrollView;
@property (nonatomic, strong) UIPageControl * lunbo_pageControl;
@property (nonatomic, strong) NSTimer * lunbo_timer;
@property (nonatomic, assign) BOOL isplay;

//轮播图
@property (nonatomic, strong) UIImageView * image1;
@property (nonatomic, strong) UIImageView * image2;
@property (nonatomic, strong) UIImageView * image3;

@property (nonatomic, strong) UILabel * title1;
@property (nonatomic, strong) UILabel * biaoqian;

@property (nonatomic, strong) UILabel * title_1;
@property (nonatomic, strong) UILabel * biaoqian_1;
//数据
@property (nonatomic, strong) NSMutableArray * arr_lunboData;


//ceshi
@property (nonatomic, assign) NSInteger count_x;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.count_x = 6;
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"资讯"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    _btnRight.hidden = YES;
    
    _btnLeft.hidden = YES;
}

//显示tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];    
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:(UITableViewStylePlain)];
    self.tabelView.backgroundColor = [UIColor whiteColor];
    self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    self.tabelView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tabelView];
    
    //注册
    [self.tabelView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cell_new"];
    
    //提前生成
    self.title1 = [[UILabel alloc] init];
    self.biaoqian = [[UILabel alloc] init];
    
    self.title_1 = [[UILabel alloc] init];
    self.biaoqian_1 = [[UILabel alloc] init];
    
    //刷新
    __unsafe_unretained UITableView * tableView = self.tabelView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data];
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
//    // 马上进入刷新状态
//    [tableView.mj_header beginRefreshing];

    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉加载
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf p_data1];
        // 结束刷新
        [tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 数据
- (void )p_data
{
    self.arr_lunboData = @[@"banner2.png",@"banner5.png",@"banner.png",@"banner4.png",@"banner3.png"].mutableCopy;
    [self p_lunbotu];
    
    self.count_x = 6;

    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tabelView reloadData];
    });
    
}

- (void )p_data1
{
    self.count_x = 10;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tabelView reloadData];
    });
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.count_x;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        if(SCREEN_WIDTH == 375)
        {
            return 180;
        }
        else if(SCREEN_WIDTH == 320)
        {
            return 150;
        }
        else
        {
            return 200;
        }
    }
    else
    {
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    if(indexPath.row == 0)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self p_lunbotu];
        
        [cell addSubview:self.lunbo_scrollView];
        [cell addSubview:self.lunbo_pageControl];
        
        UIView * view_line = [[UIView alloc] init];
        if(SCREEN_WIDTH == 375)
        {
            view_line.frame = CGRectMake(10, 180 - 1, SCREEN_WIDTH - 20, 1);
        }
        else if(SCREEN_WIDTH == 320)
        {
            view_line.frame = CGRectMake(10, 150 - 1, SCREEN_WIDTH - 20, 1);
        }
        else
        {
            view_line.frame = CGRectMake(10, 200 - 1, SCREEN_WIDTH - 20, 1);
        }
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];
    }
    else
    {
        NewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_new" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        
        return cell;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.row > 0)
    {
        NewViewController * newViewController = [[NewViewController alloc] init];
        [self showViewController:newViewController sender:nil];
    }
}

#pragma mark - 轮播图
- (void)p_lunbotu
{
    self.arr_lunboData = @[@"banner2.png",@"banner5.png",@"banner.png",@"banner4.png",@"banner3.png"].mutableCopy;
    
    if(SCREEN_WIDTH == 375)
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
    }
    else if(SCREEN_WIDTH == 320)
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    }
    else
    {
        self.lunbo_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    }
    self.lunbo_scrollView.pagingEnabled = YES;
    self.lunbo_scrollView.showsHorizontalScrollIndicator = NO;
    self.lunbo_scrollView.delegate = self;
    self.lunbo_scrollView.userInteractionEnabled = YES;
    self.lunbo_scrollView.backgroundColor = [UIColor whiteColor];
    self.lunbo_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.arr_lunboData.count + 2), 0);
    
    if(self.arr_lunboData.count == 0)
    {
        self.lunbo_scrollView.scrollEnabled = NO;
        
        self.image3 = [[UIImageView alloc] init];
        self.image3.frame = CGRectMake(SCREEN_WIDTH * 0 + 10, 10, SCREEN_WIDTH - 20, self.lunbo_scrollView.frame.size.height - 20);
        [self.image3 sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"fengjinbgpic.jpg"]];
        [self.lunbo_scrollView addSubview:self.image3];
        
        UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(10, self.image3.frame.size.height - 10 - 15 - 25, SCREEN_WIDTH - 40, 20)];
        title.textColor = [UIColor whiteColor];
        title.text = @"网销管家,最贴心的网络销售助手";
        [self.image3 addSubview:title];
        
        //
        UILabel * biaoqian = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(title.frame) + 5, SCREEN_WIDTH - 40, 15)];
        biaoqian.text = @"智顺";
        biaoqian.textColor = navi_bar_bg_color;
        biaoqian.font = [UIFont systemFontOfSize:12];
        [self.image3 addSubview:biaoqian];
    }
    else
    {
        [self lunsdsd];
        
        self.lunbo_pageControl = [[UIPageControl alloc] init];
        
        if(SCREEN_WIDTH == 320)
        {
            self.lunbo_pageControl.frame = CGRectMake(SCREEN_WIDTH - self.arr_lunboData.count * 20, self.lunbo_scrollView.frame.size.height - 30, self.arr_lunboData.count * 20, 18);
        }
        else if(SCREEN_WIDTH == 375)
        {
            self.lunbo_pageControl.frame = CGRectMake(SCREEN_WIDTH - self.arr_lunboData.count * 20 - 27.5, self.lunbo_scrollView.frame.size.height - 35, self.arr_lunboData.count * 20, 18);
        }
        else
        {
            self.lunbo_pageControl.frame = CGRectMake(SCREEN_WIDTH - self.arr_lunboData.count * 20 - 47, self.lunbo_scrollView.frame.size.height - 37, self.arr_lunboData.count * 20, 18);
        }
        //    self.lunbo_pageControl.backgroundColor = [UIColor orangeColor];
        self.lunbo_pageControl.numberOfPages = self.arr_lunboData.count;
        self.lunbo_pageControl.currentPageIndicatorTintColor = navi_bar_bg_color;
//        self.lunbo_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:202/255.0 green:218/255.0 blue:233/255.0 alpha:1];
        self.lunbo_pageControl.pageIndicatorTintColor = [UIColor whiteColor];

    }
    
    //手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [self.lunbo_scrollView addGestureRecognizer:tapGesture];
}

#pragma mark - 轮播图的点击事件
- (void)tapGesture:(id)sender
{
    NSLog(@"%ld",(long)self.lunbo_pageControl.currentPage);
    
    NewViewController * newViewController = [[NewViewController alloc] init];
    [self showViewController:newViewController sender:nil];
}

#pragma mark - 定时器的方法!
- (void)timerAction
{
    CGFloat x = self.lunbo_scrollView.contentOffset.x;
    int count = x / SCREEN_WIDTH;
    
    if(count < self.arr_lunboData.count)
    {
        count ++;
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = count - 1;
            
        }];
    }
    else if(count == self.arr_lunboData.count)
    {
        count ++;
        
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = 0;
            
        }];
    }
    else
    {
        count = 2;
        self.lunbo_scrollView.contentOffset = CGPointMake(1 * SCREEN_WIDTH, 0);
        [UIView animateWithDuration:0.7f animations:^{
            
            self.lunbo_scrollView.contentOffset = CGPointMake(count * SCREEN_WIDTH, 0);
            self.lunbo_pageControl.currentPage = count - 1;
        }];
    }
}
#pragma mark - scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = self.lunbo_scrollView.contentOffset.x;
    
    int count = x / SCREEN_WIDTH;
    
    if(count == self.arr_lunboData.count + 1)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        self.lunbo_pageControl.currentPage = 0;
    }
    else if(count == 0)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(self.arr_lunboData.count * SCREEN_WIDTH, 0);
        self.lunbo_pageControl.currentPage = self.arr_lunboData.count - 1;
    }
    else
    {
        self.lunbo_pageControl.currentPage = count - 1;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if([self.lunbo_scrollView isEqual:scrollView])
    {
        [self.lunbo_timer invalidate];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if ([scrollView isEqual:self.lunbo_scrollView]) {
        
        self.lunbo_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
}

- (void)lunsdsd
{
    self.lunbo_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.arr_lunboData.count + 2), 0);
    
    self.lunbo_pageControl.numberOfPages = self.arr_lunboData.count;
    
    self.lunbo_scrollView.scrollEnabled = YES;
    
    for (int i = 1; i <= self.arr_lunboData.count; i++)
    {
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i + 10, 10, SCREEN_WIDTH - 20, self.lunbo_scrollView.frame.size.height - 20)];
        image.image = [UIImage imageNamed:self.arr_lunboData[i - 1]];
        [self.lunbo_scrollView addSubview:image];
        
        
        UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(10, image.frame.size.height - 10 - 15 - 25, SCREEN_WIDTH - 40, 20)];
        title.textColor = [UIColor whiteColor];
        title.text = [NSString stringWithFormat:@"网销助手--%d",i];
        [image addSubview:title];
        
        //
        UILabel * biaoqian = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(title.frame) + 5, SCREEN_WIDTH - 40, 15)];
        biaoqian.text = @"智顺";
        biaoqian.textColor = navi_bar_bg_color;
        biaoqian.font = [UIFont systemFontOfSize:12];
        [image addSubview:biaoqian];
    }
    //
    self.image1 = [[UIImageView alloc] init];
    self.image1.frame = CGRectMake(SCREEN_WIDTH * 0 + 10, 10, SCREEN_WIDTH - 20, self.lunbo_scrollView.frame.size.height - 20);
    self.image1.image = [UIImage imageNamed:self.arr_lunboData.lastObject];
    [self.lunbo_scrollView addSubview:self.image1];
    
    self.title1.frame = CGRectMake(10, self.image1.frame.size.height - 10 - 15 - 25, SCREEN_WIDTH - 40, 20);
    self.title1.textColor = [UIColor whiteColor];
    self.title1.text = [NSString stringWithFormat:@"网销助手--5"];
    [self.image1 addSubview:self.title1];
    
    self.biaoqian.frame = CGRectMake(10, CGRectGetMaxY(self.title1.frame) + 5, SCREEN_WIDTH - 40, 15);
    self.biaoqian.text = @"智顺";
    self.biaoqian.textColor = navi_bar_bg_color;
    self.biaoqian.font = [UIFont systemFontOfSize:12];
    [self.image1 addSubview:self.biaoqian];
    
    //
    self.image2 = [[UIImageView alloc] init];
    self.image2.frame = CGRectMake(SCREEN_WIDTH * (self.arr_lunboData.count + 1) + 10, 10 , SCREEN_WIDTH - 20, self.lunbo_scrollView.frame.size.height - 20);
    self.image2.image = [UIImage imageNamed:self.arr_lunboData.firstObject];
    [self.lunbo_scrollView addSubview:self.image2];
    
    self.title_1.frame = CGRectMake(10, self.image1.frame.size.height - 10 - 15 - 25, SCREEN_WIDTH - 40, 20);
    self.title_1.textColor = [UIColor whiteColor];
    self.title_1.text = [NSString stringWithFormat:@"网销助手--1"];
    [self.image2 addSubview:self.title_1];
    
    self.biaoqian_1.frame = CGRectMake(10, CGRectGetMaxY(self.title_1.frame) + 5, SCREEN_WIDTH - 40, 15);
    self.biaoqian_1.text = @"智顺";
    self.biaoqian_1.textColor = navi_bar_bg_color;
    self.biaoqian_1.font = [UIFont systemFontOfSize:12];
    [self.image2 addSubview:self.biaoqian_1];
    
    
    if(self.isplay == 0)
    {
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        //轮播秒数
        self.lunbo_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        self.lunbo_pageControl.currentPage = 0;
        
        self.isplay = 1;
    }
    else
    {
//        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (self.lunbo_pageControl.currentPage + 1), 0);
        self.lunbo_scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
}


@end
